package com.lxmes.sales.service.impl;

import java.util.List;

import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.NewNumber;
import com.lxmes.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.lxmes.sales.mapper.ZcInformationMapper;
import com.lxmes.sales.domain.ZcInformation;
import com.lxmes.sales.service.IZcInformationService;

/**
 * 招彩信息Service业务层处理
 *
 * @author lx
 * @date 2023-06-21
 */
@Service
public class ZcInformationServiceImpl implements IZcInformationService
{
    @Autowired
    private ZcInformationMapper zcInformationMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询招彩信息
     *
     * @param id 招彩信息主键
     * @return 招彩信息
     */
    @Override
    public ZcInformation selectZcInformationById(Long id)
    {
        return zcInformationMapper.selectZcInformationById(id);
    }

    /**
     * 查询招彩信息列表
     *
     * @param zcInformation 招彩信息
     * @return 招彩信息
     */
    @Override
    public List<ZcInformation> selectZcInformationList(ZcInformation zcInformation)
    {
        return zcInformationMapper.selectZcInformationList(zcInformation);
    }

    /**
     * 新增招彩信息
     *
     * @param zcInformation 招彩信息
     * @return 结果
     */
    @Override
    public int insertZcInformation(ZcInformation zcInformation)
    {
        zcInformation.setCreateBy(SecurityUtils.getUsername());
        zcInformation.setDeptId(SecurityUtils.getDeptId());
        zcInformation.setOrderNumber("ZC"+NewNumber.createNewNumber(zcInformationMapper.getNewCode(DateUtils.getDate())));
        return zcInformationMapper.insertZcInformation(zcInformation);
    }

    /**
     * 修改招彩信息
     *
     * @param zcInformation 招彩信息
     * @return 结果
     */
    @Override
    public int updateZcInformation(ZcInformation zcInformation)
    {
        return zcInformationMapper.updateZcInformation(zcInformation);
    }

    /**
     * 批量删除招彩信息
     *
     * @param ids 需要删除的招彩信息主键
     * @return 结果
     */
    @Override
    public int deleteZcInformationByIds(Long[] ids)
    {
        return zcInformationMapper.deleteZcInformationByIds(ids);
    }

    /**
     * 删除招彩信息信息
     *
     * @param id 招彩信息主键
     * @return 结果
     */
    @Override
    public int deleteZcInformationById(Long id)
    {
        return zcInformationMapper.deleteZcInformationById(id);
    }
    @Override
    public String imporIteminfo(List<ZcInformation> zcInformationList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(zcInformationList) || zcInformationList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ZcInformation zcInformation : zcInformationList)
        {
            try
            {
                BeanValidators.validateWithException(validator, zcInformation);
                zcInformation.setCreateBy(operName);
                zcInformation.setUpdateBy(operName);
                this.insertZcInformation(zcInformation);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
